package com.zhihuitong.meeting.global;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.mob.MobSDK;
import com.tencent.bugly.crashreport.CrashReport;
import com.zhihuitong.meeting.dao.LocalMeetingBean;
import com.zhihuitong.meeting.dao.SQLiteUtils;
import com.zhihuitong.meeting.login.HuaWeiAuthInfo;
import com.zhihuitong.meeting.net.HttpUtil;
import com.zhihuitong.meeting.splash.SplashActivity;
import com.zhihuitong.meeting.utils.CheckedVersionUtil;
import com.zhihuitong.meeting.utils.JsonUtil;
import com.zhihuitong.meeting.utils.SPUtils;
import com.zhihuitong.meeting.utils.ToastUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.framework.loopshare.LoopShareResultListener;

/**
 * 作者： guoyuan
 * 时间 2020/2/22.
 * 文件描述：
 * 版本号：V 1.0
 **/
public class MyApplication extends Application {
    public static Context context;
    //主线程handler
    public static Handler mainHandler = new Handler();
    private static SPUtils mSPUtils;

    public static boolean isClmInit = false;


    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        mSPUtils = SPUtils.getInstance();
        HttpUtil.init(context);
        SQLiteUtils.init(context);
        //检查版本
        CheckedVersionUtil.checkedVersion(context);
        //异常上报
        CrashReport.initCrashReport(getApplicationContext(), Constant.BUGLY_ID, true);

        //java代码
        MobSDK.init(this);
        ShareSDK.prepareLoopShare(new LoopShareResultListener() {
            @Override
            public void onResult(Object o) {
                Log.i("ShareSDK", o.toString());
                try {
                    save((HashMap<String, Object>) o);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                Intent intent = new Intent(context, SplashActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }

            @Override
            public void onError(Throwable throwable) {
                Log.i("ShareSDK", throwable.toString());
            }
        });
    }

    private void save(HashMap<String, Object> customDataMap) throws UnsupportedEncodingException {
        if (customDataMap != null && !customDataMap.isEmpty()) {
            Log.i("ShareSDK", customDataMap.toString());
            String accessNumber = (String) customDataMap.get("accessNumber");
            String chairPwd = (String) customDataMap.get("chairPwd");
            String conferenceID = (String) customDataMap.get("conferenceID");
            String conferenceState = (String) customDataMap.get("conferenceState");
            String createTime = (String) customDataMap.get("createTime");
            String createUserId = (String) customDataMap.get("createUserId");
            String endTime = (String) customDataMap.get("endTime");
            String generalPwd = (String) customDataMap.get("generalPwd");
            String inviteName = (String) customDataMap.get("inviteName");
            String length = (String) customDataMap.get("length");
            String mediaTypes = (String) customDataMap.get("mediaTypes");
            String scheduserName = (String) customDataMap.get("scheduserName");
            String startTime = (String) customDataMap.get("startTime");
            String subject = (String) customDataMap.get("subject");
            String userId = (String) customDataMap.get("userId");

            //先查询，避免重复数据
            List<LocalMeetingBean> meetingBeans = SQLiteUtils.findAllMsg(conferenceID, createTime, length);
            Log.i("ShareDB", meetingBeans.toString());
            if (meetingBeans == null || meetingBeans.isEmpty()) {
                //保存
                LocalMeetingBean localMeetingBean = new LocalMeetingBean();
                localMeetingBean.setAccessNumber(null);
                localMeetingBean.setChairPwd(chairPwd);
                localMeetingBean.setConferenceID(conferenceID);
                localMeetingBean.setConferenceState(null);
                localMeetingBean.setCreateTime(Long.parseLong(createTime));
                localMeetingBean.setCreateUserId(null);
                localMeetingBean.setEndTime(URLDecoder.decode(endTime, "UTF-8"));
                localMeetingBean.setGeneralPwd(generalPwd);
                localMeetingBean.setInviteName(inviteName);
                localMeetingBean.setLength(Integer.parseInt(length));
                localMeetingBean.setMediaTypes(null);
                localMeetingBean.setScheduserName(scheduserName);
                localMeetingBean.setStartTime(URLDecoder.decode(startTime, "UTF-8"));
                localMeetingBean.setSubject(subject);
                localMeetingBean.setUserId(userId);
                boolean save = localMeetingBean.save();
                if (save) {
                    ToastUtils.showToast("保存分享数据成功");
                }
            }

//
        }
    }

//    @Override
//    protected void attachBaseContext(Context base) {
//        super.attachBaseContext(base);
//        MultiDex.install(this);
//    }

    public static String getHuaWeiToken() {
        String token = mSPUtils.getString("huawei_token");
        return token;
    }

    public static void setHuaWeiToken(String token) {
        mSPUtils.put("huawei_token", token);
    }

    public static void setUser(HuaWeiAuthInfo.UserBean user) {
        mSPUtils.put("huawei_user", JsonUtil.toJson(user));
    }

    public static HuaWeiAuthInfo.UserBean getUser() {
        String huawei_user = mSPUtils.getString("huawei_user");
        if (TextUtils.isEmpty(huawei_user)) {
            return null;
        }
        return JsonUtil.parseJsonToBean(huawei_user, HuaWeiAuthInfo.UserBean.class);
    }

    /**
     * 保存本地用户名
     *
     * @param account
     */
    public static void setLocalAccount(String account) {
        mSPUtils.put("LocalAccount", account);
    }

    public static String getLocalAccount() {
        return mSPUtils.getString("LocalAccount");
    }


    /**
     * 保存本地用户密码
     *
     * @param pwd
     */
    public static void setLocalPwd(String pwd) {
        mSPUtils.put("LocalPws", pwd);
    }

    public static String getLocalPwd() {
        return mSPUtils.getString("LocalPws");
    }

    /**
     * 保存本地用户昵称
     *
     * @param userName
     */
    public static void setLocalUserName(String userName) {
        mSPUtils.put("LocaluserName", userName);
    }

    public static String getLocalUserName() {
        return mSPUtils.getString("LocaluserName");
    }
}
